<?php 
class Code{
	private $ImgCodeBg; //画布
	private $ImgWidth; //画布长度
	private $ImgHeight;  //画布高度
	private $WordLength; //验证码字数
	private $LineLength; //线条数
	private $PowintLength; //点数量
	private $CircleLength; //圈数量
	private $FontSize;  //字体大小
/*	验证码*/
	public function ImgCodeShow(){
//		指定头部解析方式
		header("Content-type:image/png");
//		创建画布
		$this->createImg();
//		文字绘制
		$this->DrawWord();
//		验证防识别
		$this->TroubleImg();
//		生成图像
		imagepng($this->ImgCodeBg);

//		清除内存
		imagedestroy($this->ImgCodeBg);
		
}	
//	默认初始参数配置
		public function __construct($ImgWidth=NULL,$ImgHeight=NULL,$WordLength=NULL,$LineLength=NULL,$PowintLength=NULL,$CircleLength=NULL,$FontSize=NULL,$fontfile=NULL){
			$this->ImgWidth = is_null($ImgWidth) ?C('ImgWidth'):$ImgWidth;
			$this->ImgHeight = is_null($ImgHeight) ?C('ImgHeight'): $ImgHeight;
			$this->WordLength = is_null($WordLength) ?C('WordLength') :$WordLength;
			$this->LineLength = is_null($LineLength) ?C('LineLength'): $LineLength;
			$this->PowintLength = is_null($PowintLength) ?C('PowintLength') :$PowintLength;
			$this->CircleLength = is_null($CircleLength) ?C('CircleLength'): $CircleLength;
			$this->FontSize = is_null($FontSize) ?C('FontSize'):$FontSize;
			$this->fontfile = is_null($fontfile) ? C('fontfile') : $fontfile;
		}
	
		private	function createImg(){
//			创建画布
			$this->ImgCodeBg = imagecreate($this->ImgWidth, $this->ImgHeight);
//			定义画布颜色
			$whiteBg = imagecolorallocate($this->ImgCodeBg, 255, 255, 255);
		} 
		private function DrawWord(){
			$codewords = '';
//			定义字符串变量
			$seed = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnoprstuvwxyz';
//			$seed = '汉字验证码识别系统';
			for($i=0;$i<$this->WordLength;$i++){
//				随机截取字符串
				$code = $seed[mt_rand(0, strlen($seed)-1)];
				$codewords .= $code;
//				定义文字x坐标
				$x = $i * ($this->ImgWidth/$this->WordLength)+10;
//				定义文字y坐标
				$y = mt_rand(($this->ImgHeight+$this->FontSize)/2-5, ($this->ImgHeight+$this->FontSize)/2+5);
//				随机色值
				$color = imagecolorallocate($this->ImgCodeBg, mt_rand(50, 200),mt_rand(50, 200), mt_rand(50, 200));
//				组合随机x y值 和颜色   写入文字 
				imagettftext($this->ImgCodeBg, $this->FontSize, mt_rand(-30,20), $x, $y, $color, $this->fontfile, $code);
			}
			$_SESSION['code'] = strtoupper($codewords);

		}
		private function TroubleImg(){
//			线干扰
			for($i=0;$i<$this->LineLength;$i++){
//				随机颜色
				$color = imagecolorallocate($this->ImgCodeBg, mt_rand(100, 255),mt_rand(100, 255), mt_rand(100, 255));
//				画线
				imageline($this->ImgCodeBg, mt_rand(0, $this->ImgWidth),  mt_rand(0, $this->ImgHeight),  mt_rand(0, $this->ImgWidth), mt_rand(0, $this->ImgHeight), $color);
			}
//			点干扰
			for($i=0;$i<$this->PowintLength;$i++){
				$color = imagecolorallocate($this->ImgCodeBg, mt_rand(0, 200),mt_rand(0, 200), mt_rand(0, 200));
				imagesetpixel($this->ImgCodeBg, mt_rand(0,$this->ImgWidth),  mt_rand(0,$this->ImgHeight), $color);
			}
//			圈干扰
			for($i=0;$i<$this->CircleLength;$i++){
				$color = imagecolorallocate($this->ImgCodeBg, mt_rand(100, 255),mt_rand(100, 255), mt_rand(100, 255));
				imageellipse($this->ImgCodeBg, mt_rand(0, $this->ImgWidth),  mt_rand(0, $this->ImgHeight),  mt_rand(0,$this->ImgWidth), mt_rand(0, $this->ImgHeight), $color);
			}			
		}

}
//实例化调用显示
//$code =new Code();
//$code->ImgCodeShow();











?>